<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 
 * 后台登陆控制器
 * @author 66diqiu-li
 *
 */
class Admin_Index_module extends CI_Module {
	
	/**
	 * 
	 * 登陆页面
	 */
    function login(){
        $type = $this->input->get_post('type') ? $this->input->get_post('type') : 0;
        //是否已经登陆
        $admin_user_id = $this->session->userdata('admin_user_id');
        if (!empty($admin_user_id))
        {

            $redirect = $type ? site_url('hm/index') : site_url('admin/index');

            redirect($redirect);

        }
        $data['login_url'] = site_url('admin/index/tologin');
        $data['sitelogo'] = $GLOBALS['config']['sitelogo'];
        $view = $type ? 'index/login_m' : 'index/login';
        $this->load->view($view, $data);
    }
    
    /**
     * 
     * 登陆处理操作
     */
    function tologin(){
    	$arg_post = $this->input->post();
    	
    	//验证码验证
    	$this->load->library('mycaptchaclass');

    	/*if(!$this->mycaptchaclass->check($arg_post['code'])&&$arg_post['username']!='csadmin'){
    		$this->error('验证码不正确，请重新输入！',site_url('admin/index/login'),'1');
    	}
    	*/
    	if(empty($arg_post['username'])||empty($arg_post['password'])){
    		$this->error('用户名或者密码不能为空，请重新输入！',site_url('admin/index/login'),'1');
    	}
    	$this->load->model('admin.Adminuser_model');
    	if($this->Adminuser_model->login($arg_post['username'],$arg_post['password'])){
    		redirect(site_url('admin/index'));
    	}else{
    		$this->error("用户名或者密码错误，登陆失败！",site_url('admin/index/login'),1);
    	}
    }


    /**
     *
     * ajax登陆
     */
    function ajax_tologin()
    {

        $arg_post = $this->input->post();

        $refer = $this->input->get_post('refer');

        if (empty($arg_post['username']) || empty($arg_post['password']))
        {
            $this->error('用户名或者密码不能为空，请重新输入！', '', true);
        }


        $info = $this->Adminuser_model->login($arg_post['username'], $arg_post['password'], true);

        if ($info)
        {
            $this->success("登录成功", $refer, true);


        }
        else
        {


            $this->error("用户名或者密码错误，登陆失败！", '', true);

        }
    }




    /**
     * 
     * 登录退出
     */
    function logout(){
        $this->load->model('admin.Adminuser_model');
        $url = $this->input->get_post('type')? site_url("admin/index/login?type=1") : site_url("admin/index/login");
        if($this->Adminuser_model->logout()){
        	$this->success('注销成功！', $url,1);
        }
        
    }
    
    /**
     * 后台首页
     */
    public function custom_index()
    {

        $this->data['title'] = '概览';
        $this->data['enddate'] = date('Y-m-d',strtotime("today"));
        $this->data['startdate'] = date('Y-m-d',strtotime("-8 days"));

        //线路数量

        $this->data['total_shops'] = 1;
        $this->data['total_wmp'] = 2;

        //订单数量
        $this->data['order_count'] = 3;

        $this->load->view('index/main', $this->data);


    }
    
    /**
     * 
     * 获取验证码
     */
    function validate_code(){
        $this->load->library('mycaptchaclass');
        $this->mycaptchaclass->fontSize = 20;
        $this->mycaptchaclass->useCurve = false;
        $this->mycaptchaclass->entry();
    }

    /**
     * 后台完整首页
     */
    public function index()
    {

        //是否已经登陆
        $this->load->model('admin.Adminuser_model');
        $this->load->model('admin.Adminrole_model');
        $this->Adminuser_model->is_login();


        //获得左侧菜单列表
        $this->load->model('admin.Menu_model');
        $left_menu = $this->Menu_model->get_left_menu();

        $i = 1;

        $json_menu = array();
        foreach ($left_menu as $key => $val) {
            $val['id'] = $i;

            $pub = $i;
            $json_menu[$pub] = $val;
            $json_menu[$pub]['id'] = $pub;
            $json_menu[$pub]['parent_id'] = 0;
            $json_menu[$pub]['real_id'] = $val['id'];
            $i ++;

            foreach ($val['child'] as $k => $v) {
                unset($json_menu[$pub]['child'][$k]);

                $json_menu[$pub]['child'][$i] = $v;
                $json_menu[$pub]['child'][$i]['id'] = $i;
                $json_menu[$pub]['child'][$i]['parent_id'] = $pub;
                $json_menu[$pub]['child'][$i]['real_id'] = $v['id'];

                $sub1 = $i;
                $i ++;

                foreach ($v['child'] as $kk => $vv) {


                    unset($json_menu[$pub]['child'][$sub1]['child'][$kk]);

                    $json_menu[$pub]['child'][$sub1]['child'][$i] = $vv;

                    $json_menu[$pub]['child'][$sub1]['child'][$i]['id'] = $i;
                    $json_menu[$pub]['child'][$sub1]['child'][$i]['parent_id'] = $sub1;
                    $json_menu[$pub]['child'][$sub1]['child'][$i]['real_id'] = $vv['id'];


                    $i ++;
                }


            }

        }

        $role_id = $this->session->userdata('admin_role_id');
        $role = $this->Adminrole_model->get_row(array('id'=>$role_id),'name');
        $data['role'] = $role;
        $data['left_menu'] = $json_menu;
        $data['tree_menu'] = $left_menu;
        $data['sitelogo'] = $GLOBALS['config']['sitelogo'];
        $this->load->view('frame_black',$data);

    }

    public function page_header(){
        $this->load->view('public/page_header');
    }

    /**
     * 申请启用站点
     */
    function add_site(){
        $data['sel_tpl'] = isset($_GET['tpl'])?$_GET['tpl']:'bz01';
        $data['form_post'] = site_url('admin/index/add_site_save');
        $this->load->config('template');
        $data['templates'] = $this->config->config['template'];
        $this->load->view('site/add_site',$data);
    }

    /**
     * 判断是否为验证用户
     */
    function  ajax_check(){
        $arg_post = $this->input->post();
        $return = array('code'=>0,'msg'=>'');
        if(empty($arg_post['username'])||empty($arg_post['password'])){
            $return = array('code'=>2,'msg'=>'用户名或密码不能为空');
            echo json_encode($return);exit;
        }
        $user = $this->Adminuser_model->check_exist(array('name'=>$arg_post['username'],'password'=>md5($arg_post['password'])),'id,name');
        if(empty($user)){
            $return = array('code'=>3,'msg'=>'用户名或密码错误');
            echo json_encode($return);exit;
        }
        echo json_encode($return);exit;
    }

    function add_site_save(){
        $arg_post = $this->input->post();
        $form = $this->input->post('Form');


        if(empty($arg_post['username'])||empty($arg_post['password'])){
            $this->error('用户名或者密码不能为空，请重新输入！',site_url('addsite'),'1');
        }
        $user = $this->Adminuser_model->check_exist(array('name'=>$arg_post['username'],'password'=>md5($arg_post['password'])),'id,name');
        if(empty($user)){
            $this->error('用户名或者密码不正确！',site_url('addsite'),'1');
        }

        $this->load->model('admin.Site_model');
        $site = $this->Site_model->get_row(array('name'=>'未启用'));
        if(empty($site)){
            $this->error('没用可以启用的站点，请联系管理员',site_url('addsite'),'1');
        }
        $res = $this->Shopconfig_model->update_batch($site['sid'],$form);
        $this->Site_model->edit_row(array('master'=>$user['name'],'name'=>$form['sitename']),array('sid'=>$site['sid']));
        if($res){
            $this->success("操作成功",site_url('/admin/index/done_site/'.$site['sid']),3);
        }

    }

    /**
     * 启用完成的站点
     * @param $id
     */
    function done_site($id){
        $site = $this->Site_model->get_row(array('sid'=>$id));
        $data['site'] = $site;
        $this->load->view('site/done_site',$data);
    }

}